<template>
  <div class="box">
    <van-nav-bar
      title="京东登录"
      left-text="返回"
      left-arrow
      @click-left="onClickLeft"
    />
    <div class="form">
      <!-- vant表单 -->
      <van-form @submit="onSubmit">
        <van-cell-group inset>
          <van-field
            v-model="loginname"
            name="用户名"
            label="用户名"
            placeholder="用户名"
            :rules="[{ required: true, message: '请填写用户名' }]"
          />
          <van-field
            v-model="password"
            type="password"
            name="密码"
            label="密码"
            placeholder="密码"
            :rules="[{ required: true, message: '请填写密码' }]"
          />
        </van-cell-group>
        <div style="margin: 16px">
          <van-button round block type="primary" native-type="submit">
            提交
          </van-button>
        </div>
      </van-form>
    </div>
    <span>去注册</span>
    <!-- 快速注册 -->
    <div class="zhuce">
      <RouterLink to="/regist/step1">
        <van-cell is-link title="快速注册" />
      </RouterLink>
    </div>
  </div>
</template>

<script setup lang="ts">
import { longin } from "../service/user";
import { ref } from "vue";
// import { useStore } from "vuex";
import { useRouter } from "vue-router";
import { useCounterStore } from "@/stores/counter";
// let store = useStore();
let store =useCounterStore();
let router = useRouter();
let loginname = ref("");
let password = ref("");
function onClickLeft() {
  history.back();
}
function onSubmit() {
  longin({
    loginname:loginname.value,
    password: password.value,
  }).then((res) => {
    if (res.data.code === "200") {
      console.log(res);
      //存入token
      localStorage.setItem("token", res.data.data.token);
      //存入用户信息
      store.setUserInfo(res.data);
      store.chuserid(res.data.data.userid)
      //跳转登录页
      router.push("/home");
    }
  });
}
</script>

<style lang="scss" scoped>
.box {
  background-color: #f5f5f5;
    height: 100vh;
    span{
        font-size: 16px;
        padding: 10px;
    }
}
.form {
  margin: 10px 0px;
}
.zhuce{
    margin: 10px;
}
</style>
